<template>
	<view class="content wrap">
		<u-form :model="form" ref="uForm"  :rules="rules" >
			<u-form-item label="姓名" label-width="150" left-icon="account" prop="driverName">
				<u-input v-model="form.auditTime" placeholder="请输入姓名"/>
			</u-form-item>
			
			<u-form-item label="电话" label-width="150" left-icon="phone" prop="tel">
				<u-input v-model="form.tel" type="digit" placeholder="请输入电话"/>
			</u-form-item>
			
			<u-form-item label="所属公司" label-width="150">
				<u-radio-group v-model="form.gongsi">
					<u-radio 
						@change="radioChange" 
						v-for="(item, index) in worklist" :key="index" 
						:name="item.name"
					>
					{{item.name}}
					</u-radio>
				</u-radio-group>
			</u-form-item>
		</u-form>
		<view style="height: 100rpx;">
		</view>
		<u-row>
			<u-col span="12" text-align="center">
				<u-button type="primary" @click="signin">签到</u-button>
			</u-col>
			<u-col span="12" text-align="center">
				<view class="u-line-1 u-m-t-10">
					<u-button @click="signout">签退</u-button>
				</view>
			</u-col>
		</u-row>
		<view>
			<u-toast ref="uToast" />
		</view>
	</view>
</template>
<script>
	import config from '../../config.js'
	export default {
		data() {
			return {
				form:{
					auditTime:"",
					tel:"",
					gongsi:"上饶装卸一队",
					operateId:'',
				},
				worklist: [
					// {
					// 	name: '上饶装卸一队'
					// },
					// {
					// 	name: '齐河好想来装卸一队'
					// },
					// {
					// 	name: '齐河好想来装卸二队'
					// },
					// {
					// 	name: '东莞仓装卸队'
					// },
				],
			    rules: {
				},
			};
		},
		onLoad:function(open){
			//获取司机扫码的项目id
			try{
				var params = this.getQueryParams(open.q)
				if(typeof  params.operateId != 'undefined' && params.operateId != '' && params.operateId != null){
					this.form.operateId= params.operateId
				} 
			} catch (e){
				let getStorageSyncOperateId = uni.getStorageSync('operateId')
				if(typeof getStorageSyncOperateId != 'undefined' && getStorageSyncOperateId != '' && getStorageSyncOperateId != null){
					this.form.operateId= getStorageSyncOperateId
				} 
			}
			
			// 查询装卸队
			 
			this.getWorkList()
			
			
		},
		methods:{
			getQueryParams(queryString) {
				// 微信扫码得到的内容进行了一次编码，所以官方要求需要进行decodeURIComponent一次
				queryString = decodeURIComponent(queryString)
				let params = {}
				if (queryString) {
				  let queryArray = queryString.split('?')
				  if (queryArray.length > 1) {
					let query = queryArray[1]
					let array = query.split('&')
					array.map((value) => {
					  let valueArray = value.split('=') 
					  if (valueArray.length > 1) {
						// 还需要对value进行解码（可能涉及到在value为中文字符，在赋值到二维码前进行encodeURI编码）
						Object.assign(params, { [valueArray[0]]: decodeURI(valueArray[1]) })
					  }
					})
				  }
				}
				return params
			},
			getWorkList(){
				uni.request({
					method:"POST",
					url: config.root +'/rest/dict/list?code=zxd',
					success: (res) => {
						uni.hideLoading()
						if(res.success){
							this.worklist = res.data
						} 
					}
				})
			},
			// 选中某个单选框时，由radio时触发
			radioChange(e) {
				console.log(e);
			},
			signin() {
				if(typeof this.form.auditTime =='undefined'|| this.form.auditTime == null || this.form.auditTime == ''){
					uni.showToast({
						title: '请输入姓名',
						duration: 2000,
						icon:'none',
					});
					return false;
				}
				if(typeof this.form.tel =='undefined'|| this.form.tel == null || this.form.tel == ''){
					uni.showToast({
						title: '请输入电话',
						duration: 2000,
						icon:'none',
					});
					return false;
				}
				uni.showModal({
					content: '确定签到吗？',
					success: (res) => {
						if (res.confirm) {
							var that = this
							uni.getLocation({
								type:'gcj02',
								altitude:false,
								success: function (res) {
									const latitude = res.latitude;
									const longitude = res.longitude;
									uni.request({
										method:"POST",
										//anth 是否验证，非必填， false: 不需要验证 true: 需要验证,默认为true需要验证.
										custom: {auth: false},
										header: {
											'content-type': 'application/x-www-form-urlencoded',
										},
										url: config.root +'/rest/transport/signin?longitude='+longitude+'&latitude='+latitude+'&auditTime='+that.form.auditTime+'&tel='+that.form.tel+'&operateId='+that.form.operateId+'&gongsi='+that.form.gongsi,
										data:{
										},
										success: (res) => {
											if(res.data.success) {
												uni.showModal({
													title: '签到成功',
													content: '工号为'+res.data.data,
													showCancel: false,
													success: (res) => {
														if (res.confirm) {
															
														} else if (res.cancel) {
															console.log('用户点击取消');
														}
													}
												});
											} else {
												uni.showToast({
													title: res.data.message,
													duration: 2000,
													icon:'none',
												});
											}
										}
									})
								},
								fail:function(res){
									uni.showToast({
										title: '获取当前定位失败',
										duration: 2000,
										icon:'none',
									});
								},
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			signout() {
				if(typeof this.form.auditTime =='undefined'|| this.form.auditTime == null || this.form.auditTime == ''){
					uni.showToast({
						title: '请输入姓名',
						duration: 2000,
						icon:'none',
					});
					return false;
				}
				if(typeof this.form.tel =='undefined'|| this.form.tel == null || this.form.tel == ''){
					uni.showToast({
						title: '请输入电话',
						duration: 2000,
						icon:'none',
					});
					return false;
				}
				uni.showModal({
					content: '确定签退吗？',
					success: (res) => {
						if (res.confirm) {
							var that = this
							uni.getLocation({
								type:'gcj02',
								altitude:false,
								success: function (res) {
									const latitude = res.latitude;
									const longitude = res.longitude;
									uni.request({
										method:"POST",
										//anth 是否验证，非必填， false: 不需要验证 true: 需要验证,默认为true需要验证.
										custom: {auth: false},
										header: {
											'content-type': 'application/x-www-form-urlencoded',
										},
										url: config.root +'/rest/transport/signout?longitude='+longitude+'&latitude='+latitude+'&auditTime='+that.form.auditTime+'&tel='+that.form.tel+'&operateId='+that.form.operateId+'&gongsi='+that.form.gongsi,
										data:{
										},
										success: (res) => {
											if(res.data.success) {
												uni.showToast({
													title: '签退成功，您辛苦啦',
													duration: 2000
												});
											} else {
												uni.showToast({
													title: res.data.message,
													duration: 2000,
													icon:'none',
												});
											}
										}
									})
								},
								fail:function(res){
									uni.showToast({
										title: '获取当前定位失败',
										duration: 2000,
										icon:'none',
									});
								},
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	page{
			background-color: #ffffff;
	}
	.wrap {
		padding: 30rpx;
	}
</style>
